//----------------------------------Include-------------------------------------
#include  <string.h>
#include  "board.h"
#include  "crc.h"
//------------------------------------------------------------------------------
//global var defined in this file
#define  SAVE_GLOBALS
#include "save.h"
//------------------------------------------------------------------------------

//---------------------------------Global Var-----------------------------------
t_save_dev           sgt_dev;
//------------------------------------------------------------------------------

//-----------------------------------Macro--------------------------------------
#define _INIT_WORD                  (0x0520)
#define _START_ADDR                 (SAVE_END_ADDR-0x400)
//------------------------------------------------------------------------------
#define _WrNBytes(addr, pdata, len)     brd_24c256_wr_n_byte(addr, pdata, len)
#define _RdNBytes(addr, pdata, len)     brd_24c256_rd_n_byte(addr, pdata, len)
//------------------------------------------------------------------------------

//---------------------------------Static Func----------------------------------
static uint16_t _chk_sum(uint8_t* pbuf, uint16_t len)
{
  uint16_t   i;
  uint16_t   chksum;
  chksum = 0;
  for(i=0; i<len; i++) chksum += pbuf[i];
  return chksum;
}

static void _rst_vaule(void)
{
  memset(&sgt_dev, 0, sizeof(sgt_dev));
//------------------------------------------------------------------------------
  
  sgt_dev.ver              = 240501;
  sgt_dev.exist            = SAVE_EXIST_CH4   |
                             SAVE_EXIST_T     |
                             SAVE_EXIST_RH    |
                             SAVE_EXIST_IMMER |
                             SAVE_EXIST_ACC   |
                             SAVE_EXIST_BAT   |
                             SAVE_EXIST_NB    |
                             SAVE_EXIST_GPS;

  sgt_dev.dev_type         = 0x01;  //0x01--wgd
  sgt_dev.warn_ch4         = 200;   //*100       -- 0~5%     default:2%VOL
  sgt_dev.early_ch4        = 130;   //*100       -- 0~5%     default:1.3%VOL
  sgt_dev.low_temp         = 800;   //(+100)*10  -- -40~80   default:-20
  sgt_dev.high_temp        = 1600;  //(+100)*10  -- -40~80   default:60
  sgt_dev.low_bat          = 260;   //*100       -- 2.0~3.6  default:2.6V

  sgt_dev.snsr_frq_min     = 1;       //5min ~ 24hr   default:30min
  sgt_dev.data_up_frq_min  = 5;       //5min ~ 24hr   default:24hr
  sgt_dev.cfg_up_frq_hr    = 24;      //1 ~ 255 hr    default:24hr
  sgt_dev.gps_up_frq_day   = 180;     //1 ~ 255 day   default:180day
  sgt_dev.cnct_frq_hr      = 24;      //1 ~ 255 hr    default:24hr
  sgt_dev.ipport           = 10981;

  sgt_dev.pswd_user        = 0;
  sgt_dev.pswd_root        = 553416;

  sgt_dev.ipv4             = (115<<24)+(236<<16)+(153<<8)+174;    //hsk test ip
  sgt_dev.dev_addr         = 0x99;

//------------------------------------------------------------------------------
  sgt_dev.u16InitWd = _INIT_WORD;
  sgt_dev.u16check  = crc16( (uint8_t*)(&sgt_dev),
                             (uint8_t*)(&sgt_dev.u16check) - (uint8_t*)(&sgt_dev) );
  
  _WrNBytes(_START_ADDR, (uint8_t*)&sgt_dev, sizeof(sgt_dev));
}
//---------------------------------Public Func----------------------------------
uint32_t save_dev_write(void)
{
  uint16_t   ck;
  
  ck = crc16( (uint8_t*)(&sgt_dev),
              (uint8_t*)(&sgt_dev.u16check) - (uint8_t*)(&sgt_dev) );
  
  if(sgt_dev.u16check != ck){
    sgt_dev.u16check = ck;
    _WrNBytes(_START_ADDR, (uint8_t*)&sgt_dev, sizeof(sgt_dev));
    return 1;
  }
  return 0;
}

void save_dev_read(void)
{
  uint16_t   ck;
  
  _RdNBytes(_START_ADDR, (uint8_t*)&sgt_dev, sizeof(sgt_dev));
  
  if(sgt_dev.u16InitWd != _INIT_WORD){
    _rst_vaule();
  }
  ck = crc16( (uint8_t*)(&sgt_dev),
              (uint8_t*)(&sgt_dev.u16check) - (uint8_t*)(&sgt_dev) );
  
  if(ck != sgt_dev.u16check){
    _rst_vaule();
  }
}

void save_dev_reset(void)
{
  _rst_vaule();
}
//-------------------------------------EOF--------------------------------------

